package von.seiji.cn.test;

import cn.hutool.core.util.StrUtil;
import com.google.zxing.*;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeReader;
import com.google.zxing.qrcode.QRCodeWriter;

import org.junit.Test;

import javax.swing.filechooser.FileSystemView;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;

public class T8 {

    @Test
    public void test01(){
        try {
            Process tesseract1 = Runtime.getRuntime().exec( "cmd.exe /f tesseract E:\\Seiji926\\software\\Tesseract-OCR\\a.png result -l chi_sim");
            /*InputStream inputStream = tesseract1.getInputStream();
            byte[] bytes = new byte[1024];
            for (int i;(i=inputStream.read(bytes))!=-1;){
                System.out.println(new String(bytes,"gbk"));
            }*/
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static String trimWhitespace(String str) {
        if (StrUtil.isBlank(str)) {
            return str;
        } else {
            int beginIndex = 0;
            new String();
            int endIndex;
            for(endIndex = str.length() - 1; beginIndex <= endIndex && Character.isWhitespace(str.charAt(beginIndex)); ++beginIndex) {

            }

            while(endIndex > beginIndex && Character.isWhitespace(str.charAt(endIndex))) {
                --endIndex;
            }

            return str.substring(beginIndex, endIndex + 1);
        }
    }
    @Test
    public void test02(){
        String s1 = "   sd lskf skdfjdls sdkjfklds fds  dsjfkdslf s   ";
        String s = trimWhitespace(s1);
        System.out.println(s);
        String trim = s1.trim();
        System.out.println("trim = " + trim);
    }

    public void tst() throws WriterException, IOException, FormatException, ChecksumException, NotFoundException {
        QRCodeWriter qrCodeWriter = new QRCodeWriter();

        BitMatrix bitMatrix = qrCodeWriter.encode("text", BarcodeFormat.QR_CODE, 200, 200);

        Path path = FileSystems.getDefault().getPath("filePath");

        MatrixToImageWriter.writeToPath(bitMatrix, "PNG", FileSystemView.getFileSystemView().getHomeDirectory().toPath());

        QRCodeReader qrCodeReader = new QRCodeReader();
        Result decode = qrCodeReader.decode(null);
        String text = decode.getText();
        System.out.println("text = " + text);
    }



}
